import { createRouter, createWebHistory } from 'vue-router'
import LoginPage from '@/views/login/LoginContainer.vue'
import HomeLayout from '@/views/homeLayout/HomeLayout.vue'
import Recommend from '@/views/recommend/Recommend.vue'
import Vicinity from '@/views/vicinity/Vicinity.vue'
import Publish from '@/views/publish/Publish.vue'
import Message from '@/views/message/Message.vue'
import User from '@/views/user/User.vue'
import { useUserStore } from '@/stores/index.js'
import { ElMessage } from 'element-plus'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/', redirect: '/homelayout'},
    {
      path: '/homelayout',
      redirect:'/recommend',
      component:HomeLayout,
      children:[
        {path: "/recommend",component: Recommend},
        {path: "/publish",component: Publish,  meta: { requiresAuth: true }},
        {path: "/vicinity",component: Vicinity,meta: { requiresAuth: true }},
        {path: "/message",component: Message,  meta: { requiresAuth: true }},
        {path: "/user",component: User,  meta: { requiresAuth: true }},
      ]
    },
  ]
})
// 全局前置路由守卫
router.beforeEach((to,from,next) => {
  const userStore = useUserStore()
  // 既没有token，访问的又不是login，就要拦截
  if ( !userStore.token && to.meta.requiresAuth) {
    ElMessage.warning("需要登录后才能访问~")
    next(false) // 中断导航
  }else{
    next()
  }
}) 

export default router
